import read_data as rd
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import numpy as np
import pymc as pm



all_aft = rd.read_aft(None,False)
f = open("sg.csv",'w')
f.write("sample, sample_age, discrepancy, zeta_g, zeta_g_1s, ns, a, p, dpar, q_age, sg_age, sg_age_1s\n")

for aft in all_aft:
    #print aft
    #try:
    sa = aft.sample_age[0]
    
    sga, sga_sd = zip(*aft.sg_ages)
    disc = aft.sample_age_discrepancy(plot=False)
    zeta_g = aft.zeta_ms[0]*aft.g
    zeta_g_1s=aft.zeta_ms[1]*aft.g
    ns=aft.counts['Ns']
    a=aft.counts['A']
    p=aft.counts['P']
    qa = aft.quick_ages
    dpar=aft.counts['Dpar']
    for i in range(aft.n_cnt):
	f.write("%s, %f, %f, %f, %f, %i, %f, %f, %f, %f, %f, %f\n" % \
			(aft.name, sa, disc, zeta_g, zeta_g_1s, ns[i], a[i], p[i], dpar[i], qa[i], sga[i], sga_sd[i]   ))
    #aft.sg_ages_discrepancy(plot=True)
    #except pm.ZeroProbability: print "Failed"

f.close()



#for aft in all_aft:
#    plt.figure()
#    dpars=aft.counts['Dpar']
#    sample=aft.name
#    Dpar=np.mean(dpars)
#    Dpar_std=np.std(dpars)
#    plt.hist(dpars, normed=True, facecolor='gray', alpha=0.75)
#    x = np.linspace(min(dpars),max(dpars))
#    y = mlab.normpdf(x, Dpar, Dpar_std)
#    plt.plot(x, y, 'b-', linewidth=1)
#    plt.xlabel('Dpar')
#    plt.ylabel('Probability')
#    plt.title('Sample %s, N = %i' % (sample,len(dpars)))
#    #plt.axis([40, 160, 0, 0.03])
#    plt.grid(True)
#    plt.savefig("Dpar_%s.png"%sample)

    
